package com.teamb6.shop.controller.front;

import com.teamb6.shop.domain.Comment;
import com.teamb6.shop.domain.Goods;
import com.teamb6.shop.domain.Indent;
import com.teamb6.shop.domain.User;
import com.teamb6.shop.service.CommentService;
import com.teamb6.shop.service.GoodsService;
import com.teamb6.shop.service.IndentService;
import com.teamb6.shop.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("comments")
public class CommentController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private IndentService indentService;

    //跳转到用户商品评论界面
    @GetMapping("/evaluation")
    public String commentGoods(Integer orderid, Model model){
        List<Goods> goodsList = goodsService.getIndentGoods(orderid);
        model.addAttribute("goodList",goodsList);
        model.addAttribute("orderid",orderid);
        return "person/commentlist";
    }

    //增加商品评论
    @PostMapping("/addComment")
    @ResponseBody
    public Msg addComment(Comment comment, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            return Msg.fail("评论失败");
        }
        comment.setUserid(user.getUserid());
        Date date = new Date();
        comment.setCommenttime(date);
        commentService.insertSelective(comment);
        return Msg.success("评论成功");
    }

    //评论完成后将订单改为已完成
    @PostMapping("/commentFin")
    @ResponseBody
    public void commentFin(Integer orderid){
        Indent indent = indentService.selectByPrimaryKey(orderid);
        indent.setIscomplete(true);
        indentService.updateIndentByKey(indent);
    }
}
